﻿using FO.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Bussiness.User;
using FOModels.User;
namespace FO.Controllers
{
    public class AccountController :FOSystem.CultureHelper.CultureController
    {
        //
        // GET: /Login/
        public ActionResult LogOn(string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                   
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            return View();
        }
        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult LogOn(LogonModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {

                    FormsAuthentication.SetAuthCookie(model.UserName, model.isRemember);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                       
                        return Redirect(returnUrl);

                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("InvalidLogin", "Email hoặc mật khẩu không đúng!");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }


        public ActionResult Login(string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {

                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            return View();
        }
        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult Login(LogonModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {

                    FormsAuthentication.SetAuthCookie(model.UserName, model.isRemember);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {

                        return Redirect(returnUrl);

                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("InvalidLogin", "Email hoặc mật khẩu không đúng!");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/LogOff

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/Register
        public ActionResult SearchAccountByEmail()
        {
            UserBLL bll = new UserBLL();
            FOModels.User.User u = bll.seachUser(User.Identity.Name);
            return PartialView(u);
        }
        public ActionResult AccountLogin()
        {
            UserBLL bll = new UserBLL();
            FOModels.User.User u = bll.seachUser(User.Identity.Name);
            return PartialView(u);
        }
        public ActionResult Register(string Email,string Name,string Phone,string dob)
        {
            int result=0;
             Fcom.IDLib.SaveMoneyID.AccountsynClient client = new Fcom.IDLib.SaveMoneyID.AccountsynClient();
             if (client.checkemailexists(Email) == false)
             {
                 client.CreateAccountFl(Email,Phone,Name,true,1,DateTime.Parse(dob));
                 result = 1;
                 return Json(result, JsonRequestBehavior.AllowGet);
             }
             else
             {
                 result = -1;
                 return Json(result, JsonRequestBehavior.AllowGet);
             }
            
        }

     

            

    }
}
